<template>
  <div class="contain-report">
    <div class="report-title">案例类别</div>
    <div class="report-search">
      <el-input
          size="small"
          style="width: 200px"
          placeholder="请输入"
          clearable
          @clear="getList"
          v-model="form.name"
      >
        <i slot="suffix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="getList"></i>
      </el-input>
    </div>
    <div class="report-add">
      <div class="add-box" @click="upload = true">
        <i class="el-icon-circle-plus-outline"></i>
        <span>上传案例类别</span>
      </div>
    </div>
    <div class="report-list" v-if="list.length !== 0" v-for="item in list" :key="item.id">
      <div class="list-box">
        <h3 style="margin-bottom: 10px">{{ item.name }}</h3>
      </div>
      <div class="right">
        <div class="right-content">

        </div>
        <div class="right-date">
          <span style="margin-bottom: 10px">上传时间:</span>
          <span>{{ item.createTime }}</span>
        </div>
        <div class="right-content">

        </div>
        <div class="btns">
          <el-button type="text" @click="openDialog(item)">编辑</el-button>
          <el-button type="text" @click="deleteCaseCategory(item.id)">删除</el-button>
        </div>

      </div>
    </div>
    <div class="report-nolist" v-if="list.length === 0">
      <el-empty description="暂无数据"></el-empty>
    </div>

    <div class="report-page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="1"
          :page-sizes="[3]"
          :page-size="3"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog
        :show-close="false"
        :visible.sync="upload"
        width="40%"
    >
      <div class="dialog-box">
        <div class="box-top">添加案例类别</div>
        <div class="box-body">
          <el-form label-width="80px">
            <el-form-item label="类别名称:">
              <el-input placeholder="请输入案例类别名称" v-model="uploadForm.name"></el-input>
            </el-form-item>
          </el-form>
          <div class="form-btn">
            <el-button type="primary" size="small" @click="subMitForm" style="margin-right: 20px">提 交</el-button>
            <el-button size="small" @click="consoleForm">取 消</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
    <el-dialog
        :show-close="false"
        :visible.sync="editDialog"
        width="40%"
    >
      <div class="dialog-box">
        <div class="box-top">编辑案例类别</div>
        <div class="box-body">
          <el-form label-width="80px">
            <el-form-item label="类别名称:">
              <el-input placeholder="请输入案例类别名称" v-model="editForm.name"></el-input>
            </el-form-item>
          </el-form>
          <div class="form-btn">
            <el-button type="primary" size="small" @click="subMitEdit" style="margin-right: 20px">提 交</el-button>
            <el-button size="small" @click="editDialog = false">取 消</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getRecipeClassPage, getRecipeClassFindList, getRecipeClassDeleteById, EditRecipeClass,AddRecipeClass} from "../../api/laboratory";

export default {
  name: "caseCategoryThere",
  data() {
    return {
      form: {
        name: '',
        pageIndex: 1,
        pageSize: 3
      },
      list: [],
      total: null,
      upload: false,
      uploadForm: {
        name: ''
      },
      editDialog: false,
      editForm: {}
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    consoleForm() {
      this.upload = false
      this.disabled = false
      this.uploadForm = {}
    },
    async getList() {
      const {data} = await getRecipeClassPage(this.form)
      if (data.code === 200) {
        this.list = data.data.data
        this.total = data.data.total
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.form.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.form.pageIndex = val
      this.getList()
    },
    async subMitForm() {
      if (!this.uploadForm.name) {
        this.$message.error('请输入类别名称!!')
      } else {
        const {data} = await AddRecipeClass(this.uploadForm)
        if (data.code === 200) {
          this.$message.success(data.message)
          this.uploadForm = {}
          this.upload = false
          await this.getList()
        }
      }
    },
    deleteCaseCategory(id) {
      this.$confirm('此操作将永久删除该案例类别, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        getRecipeClassDeleteById(id).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.getList()
          }
        })
      }).catch(() => {
        this.$message.warning('已取消删除')
      });
    },
    openDialog(item) {

      const {name, id} = item
      this.editForm = {name, id}
      console.log(this.editForm)
      this.editDialog = true
    },
    async subMitEdit() {
      const {data} = await EditRecipeClass(this.editForm)
      if (data.code === 200) {
        this.$message.success(data.message)
        this.editForm = {}
        this.editDialog = false
        await this.getList()
      }
    },
  }
}
</script>

<style scoped lang="less">
.contain-report {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;

  .dialog-box {
    width: 100%;
    min-height: 50vh;
    border-radius: 5px;
    overflow: hidden;

    .box-top {
      width: 100%;
      height: 10vh;
      background-image: linear-gradient(to left, #948af1 0%, #c193db 50%, #ed9bc6 100%);
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 26px;
      color: #fff;
    }

    .box-body {
      width: 100%;
      height: 80%;
      padding: 5% 10%;
      box-sizing: border-box;
      background-color: #ffffff;

      /deep/ .el-input__inner {
        border: none;
        background-color: #ecedef;
      }

      /deep/ .el-textarea__inner {
        border: none;
        background-color: #ecedef;
      }

      .form-btn {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .file-list {
        margin-top: 20px;
        width: 100%;

        .file-box {
          margin-top: 10px;
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: space-between;

          .left {
            width: 40%;
            display: flex;
            align-items: center;
            justify-content: space-between;
          }
        }
      }
    }
  }

  .report-title {
    font-size: 22px;
    color: #666;
  }

  .report-search {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;

    p {
      margin: 0 10px;
      box-sizing: border-box;

      &:hover {
        cursor: pointer;
      }
    }
  }

  .report-list {
    width: 100%;
    margin: 20px 0;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 0 5px 0 #ccc;
    padding: 20px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .list-box {
      width: 50%;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: flex-start;
    }

    .right {
      width: 50%;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .right-content {
        width: fit-content;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        justify-content: space-around;
        color: #999;
      }

      .right-date {
        width: fit-content;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        justify-content: space-around;
        color: #999;
      }

      .btns {
        width: 20%;
        padding: 0 5%;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
      }
    }

  }

  .report-nolist {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .report-add {
    width: 100%;
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .add-box {
      width: 100%;
      border-radius: 5px;
      border: 1px dashed #6ba9ff;
      padding: 5px;
      display: flex;
      align-items: center;
      justify-content: center;

      i {
        font-size: 20px;
        color: #6ba9ff;
        margin-right: 5px;
      }

      span {
        font-size: 16px;
        color: #6ba9ff;
      }

      &:hover {
        cursor: pointer;
      }
    }
  }

  .report-page {
    position: absolute;
    bottom: 10px;
    left: 50%;
    width: 100%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
